<template>
  <div class="personal-wrap">
    <el-dialog
      v-loading="loading"
      :element-loading-text="loadingText"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      custom-class="personal-dialog"
      :visible.sync="personDialogFlag"
      :close-on-click-modal="false"
      :lock-scroll="false"
      :fullscreen="true"
      :show-close="false"
    >
      <div class="explain-top">
        <div class="explain-top-content">
          <h1>云木屋</h1>
          <span>合同服务</span>
        </div>
      </div>
      <div class="personal-dialog-content">
        <div>
          <div v-show="stepNum!=4" class="dialog-step">
            <div class="step-item step-one step-active">
              <span>1</span>
              <p>开通电子合同服务</p>
            </div>
            <div class="step-item step-two" :class="{'step-active':stepNum > 1 }">
              <span>2</span>
              <p>申请CA证书</p>
            </div>
            <div class="step-item step-three" :class="{'step-active':stepNum > 2}">
              <span>3</span>
              <p>开通成功</p>
            </div>
          </div>
          <!-- 开通电子合同服务 -->
          <div v-if="stepNum==1" class="personal-form">
            <img src="@/assets/authentication/contract1.png" alt />
            <h1>电子合同服务开通</h1>
            <p>开通电子合同服务，保障买卖双方权益，合法合规 合同永不丢失、更安全省心</p>
            <el-button @click="_ssqDoSsq">立即开通</el-button>
          </div>
          <!-- 申请CA证书 -->
          <div v-else-if="stepNum==2" class="personal-form">
            <img src="@/assets/authentication/contract2.png" alt />
            <h1>申请CA证书</h1>
            <p>开通电子合同服务，保障买卖双方权益，合法合规 合同永不丢失、更安全省心</p>
            <el-button @click="_ssqDoCa">申请CA证书</el-button>
          </div>

          <!-- 开通成功 -->
          <div v-else-if="stepNum==3" class="personal-form">
            <img class="success-img" src="@/assets/icon-success.png" alt />
            <h1>开通成功</h1>
            <p class="success-p">赶紧去签合同吧，创建属于你的云木屋</p>
            <el-button @click="_ssqSignContract">去签署合同</el-button>
          </div>
        </div>
        <div class="tips-wrap">
          <h2>
            <span>*</span>温馨提示
          </h2>
          <p>1.合同签署依据《合同法》、《电子签名法》流程规范，嵌入国家认证CA机构证书系统，并采用第三方授时及防篡改技术，确保电子合同具有与传统纸质合同等同的法律效力；</p>
          <p>2."上上签"- 杭州尚尚签网络科技有限公司，为用户提供电子签名与电子合同服务；</p>
          <p>3.用户在木头云平台可免费开通电子合同服务，基于在平台交易的订单可免费签署电子合同；</p>
          <p>4.木头云的电子合同技术服务由杭州尚尚签网络科技有限公司提供。</p>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  ssqCheck,
  ssqDoSsq,
  ssqDoCa,
  ssqSignContract
} from "@/api/authentication";
export default {
  data() {
    return {
      personDialogFlag: true,
      stepNum: 1,
      loading: false,
      loadingText: "正在开通上上签..."
    };
  },
  mounted() {
    this._ssqCheck();
  },
  methods: {
    // 上上签检测
    _ssqCheck() {
      ssqCheck().then(res => {
        if (res.code == 10031) {
          this.stepNum = 1;
        } else if (res.code == 10032) {
          this.stepNum = 2;
        } else if (res.code == 10033) {
          this.stepNum = 3;
        } else {
          this.$router.push({ name: "contractSign" });
        }
      });
    },
    // 开通上上签
    _ssqDoSsq() {
      this.loadingText = "正在开通上上签...";
      this.loading = true;
      ssqDoSsq().then(res => {
        if (res.code == 1) {
          setTimeout(() => {
            this.$message.success("上上签开通成功");
            this.loading = false;
            this.stepNum = 2;
          }, 1000);
        } else {
          this.loading = false;
          this.$message.error(res.msg || "请求失败");
        }
      });
    },
    // 申请CA证书
    _ssqDoCa() {
      ssqDoCa().then(res => {
        this.loadingText = "正在申请CA证书...";
        this.loading = true;
        if (res.code == 1) {
          setTimeout(() => {
            this.$message.success("CA证书申请成功");
            this.loading = false;
            this.stepNum = 3;
          }, 1000);
        } else {
          this.loading = false;
          this.$message.error(res.msg || "请求失败");
        }
      });
    },
    // 签署电子协议
    _ssqSignContract() {
      // ssqSignContract().then(res => {
      //   if (res.code == 1) {
      //     window.open(res.data.url, "_self");
      //   } else {
      //     this.$message.error(res.msg || "请求失败");
      //   }
      // });
      this.$router.push({ name: "contractSign" });
    }
  }
};
</script>

<style lang='scss'>
.personal-wrap {
  .personal-dialog {
    background: #f0f0f0;
    .el-dialog__header {
      padding: 0;
    }
    .el-dialog__body {
      padding: 0;
      .personal-form {
        .el-form {
          .el-form-item {
            .el-form-item__content {
              .el-input__inner {
                width: 300px;
              }
            }
          }
        }
      }
    }
  }
}
</style>


<style lang='scss' scoped>
.personal-wrap {
  .personal-dialog {
    background: #f0f0f0;

    .explain-top {
      height: 60px;
      border-bottom: 1px solid #f1f1f1;
      box-shadow: 0px 2px 5px #f1f1f1;
      background: #fff;
      .explain-top-content {
        width: 1200px;
        margin: 0 auto;
        position: relative;
        h1 {
          display: inline-block;
          line-height: 60px;
          font-size: 30px;
          font-weight: bold;
          background-image: linear-gradient(0deg, #ff9303 0%, #fe6018 100%);
          -webkit-background-clip: text;
          color: transparent;
          margin: 0;
        }
        span {
          display: inline-block;
          vertical-align: top;
          margin-left: 30px;
          font-size: 16px;
          font-weight: 400;
          color: rgba(51, 51, 51, 1);
          position: absolute;
          top: 50%;
          left: 80px;
          transform: translateY(-50%);
        }
      }
    }
    .personal-dialog-content {
      width: 1200px;
      min-height: 700px;
      margin: 0 auto;
      background: #fff;
      overflow: hidden;
      margin-top: 20px;
      .dialog-step {
        width: 50%;
        margin: 0 auto;
        display: flex;
        justify-content: space-between;
        text-align: center;
        margin-top: 80px;
        margin-bottom: 70px;
        .step-item {
          width: 130px;
          font-size: 16px;
          span {
            padding: 2px 15px;
            background: #bcbcbc;
            border-radius: 11px;
            color: #fff;
            position: relative;
            &::before {
              content: "";
              position: absolute;
              width: 80px;
              height: 1px;
              background: #cccccc;
              top: 50%;
              transform: translateY(-50%);
              left: -90px;
            }
            &::after {
              content: "";
              position: absolute;
              width: 80px;
              height: 1px;
              background: #cccccc;
              top: 50%;
              transform: translateY(-50%);
              right: -90px;
            }
          }
          p {
            color: #bcbcbc;
          }
          &.step-active {
            span {
              background: #ff7f5b;
              &::after {
                background: #ff7f5b;
              }
              &::before {
                background: #ff7f5b;
              }
            }
            p {
              color: #ff7f5b;
            }
          }
          &:first-of-type {
            span {
              &::before {
                display: none;
              }
              &::after {
                background: #ff7f5b;
              }
            }
          }
          &:last-of-type {
            span {
              &::after {
                display: none;
              }
            }
          }
        }
      }
      .personal-form {
        text-align: center;
        img {
          width: 112px;
          height: 93px;
          &.success-img {
            width: 80px;
            height: 100px;
          }
        }
        h1 {
          font-size: 20px;
          font-weight: bold;
          color: #111;
          line-height: 0;
          margin: 35px 0 25px 0;
        }
        p {
          font-size: 14px;
          font-weight: 400;
          color: rgba(153, 153, 153, 1);
          line-height: 24px;
          display: block;
          width: 308px;
          margin: 0 auto;
          margin-bottom: 30px;
          &.success-p {
            font-size: 16px;
            span {
              color: #ff7f5b;
            }
          }
        }
        .el-button {
          width: 130px;
          height: 40px;
          border-color: #ff7f5b;
          color: #ff7f5b;
        }
      }
      .personal-setPassword {
        text-align: center;
        font-size: 16px;
        color: #404040;
        margin-top: 200px;
        cursor: pointer;
      }
      .personal-result {
        text-align: center;
        img {
          width: 80px;
          height: 100px;
          margin: 110px 0 20px 0;
        }
        h1 {
          font-size: 20px;
          font-weight: bold;
          color: #111;
          margin-bottom: 30px;
        }
        p {
          font-size: 16px;
          font-weight: 400;
          color: #999;
          cursor: pointer;
        }
      }
      .tips-wrap {
        margin: 80px 20px 40px 20px;
        padding: 15px 20px 0 20px;
        border-top: 1px solid #f2f2f2;
        h2 {
          font-size: 14px;
          font-weight: 400;
          color: #333333;
          span {
            color: #fe4850;
          }
        }
        p {
          color: #999;
        }
      }
    }
  }
}
</style>